package com.drodds.game;

import java.util.Locale;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import java.util.ArrayList;

public class Reel {
	private static final int reelSpeed = 1000;
	
	public int reelNum;
	
	private int numStops;
	private int numDifferentValues;
	private WeightedRandomSelector<AllValues> valueSelector;
	private ArrayList<AllValues> selectedValues = new ArrayList<AllValues>();
	private int lastValue = 0;
	private String lastName = AllValues.ZERO.GetName();
	private Resources resources;
	private Drawable background;
	private int drawOffset = 0;
	private int firstSpin = 0;
	public boolean stopped = true;
	
	
	public Reel(Resources res, int stops, int values){
		numStops = stops;
		numDifferentValues = values;
		resources = res;
		background = resources.getDrawable(res.getIdentifier("reelbackground", "drawable", "com.drodds.game"));
		
		ArrayList<WeightedItem<AllValues>> valList = new ArrayList<WeightedItem<AllValues>>();
		for (AllValues v : AllValues.values()) {
			valList.add(new WeightedItem<AllValues>(v.GetVal(), v));
		}
		
		valueSelector = new WeightedRandomSelector<AllValues>(valList);
	}
	
	private void CalculateReels(){
		AllValues tempV;
		
		if (selectedValues.size() > 0) {
			tempV = selectedValues.get(0);
			selectedValues.clear();
			selectedValues.add(tempV);
		}
		
		for (int i = 0; i < numStops; i++) {
			selectedValues.add(valueSelector.select().getItem());
		}
	}
	
	public void update(float deltaTime) {
		if (!stopped) {
			drawOffset += Math.round(reelSpeed * deltaTime);
			SetLastValue();
		}
	}
	
	public void Stop() {
		for (int i = 2; i < selectedValues.size(); i++) {
			selectedValues.remove(i);
		}
	}
	
	public void draw(Canvas c, Rect bounds) {
		String firstName = "";
		int firstPositionID = -1;
		Drawable d1 = null;
		
		background.setBounds(bounds);
		background.draw(c);
		
		if (selectedValues.size() <= 1) {
			if ((selectedValues.size() > 0) && (firstSpin > 0)) {
				firstName = selectedValues.get(0).GetName().toLowerCase(Locale.ENGLISH);
				firstPositionID = resources.getIdentifier("position_" + firstName, "drawable", "com.drodds.game");
				d1 = resources.getDrawable(firstPositionID);
				d1.setBounds(bounds);
				d1.draw(c);
			}
			
			drawOffset = 0;
			stopped = true;
			return;
		}
		
		firstName = selectedValues.get(0).GetName().toLowerCase(Locale.ENGLISH);
		firstPositionID = resources.getIdentifier("position_" + firstName, "drawable", "com.drodds.game");
		d1 = resources.getDrawable(firstPositionID);
		
		Rect toDraw = new Rect(bounds.left, bounds.top + drawOffset, bounds.right, bounds.bottom + drawOffset);
		
		String secondName = "";
		int secondPositionID = -1;
		
		Drawable d2 = null;
		
		Bitmap src1 = Bitmap.createScaledBitmap(((BitmapDrawable) d1).getBitmap(), bounds.width(), bounds.height(), false);
		Bitmap src2 = null;
		
		if (selectedValues.size() > 1) {
			secondName = selectedValues.get(1).GetName().toLowerCase(Locale.ENGLISH);
			secondPositionID = resources.getIdentifier("position_" + secondName, "drawable", "com.drodds.game");
			d2 = resources.getDrawable(secondPositionID);
			src2 = Bitmap.createScaledBitmap(((BitmapDrawable) d2).getBitmap(), bounds.width(), bounds.height(), false);
			
		}
		
		if (toDraw.top >= bounds.bottom){
			selectedValues.remove(0);
			drawOffset = 0;
			Bitmap dest2 = Bitmap.createBitmap(src2, 0,0,src2.getWidth(),src2.getHeight());
			c.drawBitmap(dest2, bounds.left, bounds.top, null);
		}
		else {
			Bitmap dest1 = Bitmap.createBitmap(src1, 0,0,src1.getWidth(), src1.getHeight() - (toDraw.bottom - bounds.bottom));
			c.drawBitmap(dest1, toDraw.left, toDraw.top, null);
			
			if (selectedValues.size() > 1) {
				int height = toDraw.bottom - bounds.bottom;
				if (height > 0) {
					Bitmap dest2 = Bitmap.createBitmap(src2, 0,src2.getHeight() - height,src2.getWidth(), height);
					c.drawBitmap(dest2, bounds.left, toDraw.top - height, null);
				}
			}	
		}
	}
	
	public void Spin(){
    	CalculateReels();
    	stopped = false;
    	firstSpin++;
	}
	
	public void SetLastValue() {
		if (selectedValues.size() > 0) {
			System.out.println(String.format("Reel %d setting last value = %d", reelNum, selectedValues.get(0).GetVal()));
			lastValue = selectedValues.get(0).GetVal();
		}
		else {
			lastValue = 0;
		}
	}
	
	public int GetLastValue() {
		return lastValue;
	}
	
	public String GetLastName() {
		return lastName;
	}
}
